// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var hashJump = require("../hash/hashJump");
	var validate = require("validate");
	var dialog = require("dialog");
	var router = require("../hash/pageRouter");
	var css_modify_advertisement = require("./advertisement_modify.css");
	var imgSrc;
	var countId;
	var spaceId;
	var init = function(page) {
		spaceId = page.params.spaceId||base.getUrlParam("spaceId");
		initevent();
//		hashJump.init();
		getAdvert();
		getAdSpaceInfo();
	};
	var initevent = function() {
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
		
		//修改顶部栏的选中状态
		$(".nav_hover", window.parent.document).removeClass("head_link_1");
		$("#company", window.parent.document).addClass("head_link_1");
		
		var html_modify = require("./adModify-modify.html");
		var html_delete = require("./adModify-delete.html");

		$(".column_show").find("#ad_detail").attr("arg", "spaceId=" + spaceId);
		$("#uploadImg").unbind().on('change', function() {
			uploadPicture();
		});
		//点击上传按钮
		$(".ful_content").on("click", ".upload_button", function() {
			clickUpload();
		});
		//点击上传按钮
		$("#main").on("click", "#ad_manage", function() {
			router.route("adManage/platform_main_advertisement_manager.html");
		});
		
		//修改广告的点击事件
		$(".ad_list").on("click", ".modify_button", function() {
			$("#popdialog").html(html_modify);
			var $this = $(this).parents(".ad_img_display_ok");
			var adId = $this.attr("adId");
			var fileName = $this.find(".fileName").html();
			var sequenze = $this.find(".sequenze").html();
			var link = $this.find(".url_set").html();
			var fileUid = $this.find("#fileUid").val();
			
			var modify_ad_dialog = $("#modify_ad_dialog");
			$("#fileName").html(fileName);
			$("#sequenze").val(sequenze);
			$("#link").val(link);
			dialog.showDialog(modify_ad_dialog, function() {
				modifyAd(adId,fileUid);
			});
		});
		//删除广告的点击事件
		$(".ad_list").on("click", ".delete_button", function() {
			var adId = $(this).parent().parent().parent().attr("adid");
			$(this).parent().parent().parent().remove();
			deleteAd(adId);
		});
		
		$(".button_layout").on("click", "#save_btn", function() {
			updateAdSpaceInfo();
		});
	};
	var modifyAd = function(adId,fileUid) {
		var fileNa = $("#fileName").html();
		var seqNo = $("#sequenze").val();
		var url = $("#link").val();
		base.fnAjax({
			action: "updateAd.json", //接口名称
			oData: {
				adId: adId,
				fileUuid: fileUid,
				fileName: fileNa,
				seqNo: seqNo,
				url: url
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					base.alert(1, "修改成功！");
					dialog.hideDialog($("#modify_ad_dialog"));
					router.route("adModify/advertisement_modify.html?spaceId="+spaceId);
				} else {
					util.alert("修改广告信息失败！");
				}
				dialog.hideDialog(modify_ad_dialog);
			}
		});
	};
	//更新广告位信息
	var updateAdSpaceInfo = function() {
		var promptDialog = validate.validateInput($(".daModifyDetails"));
		if (promptDialog.length != 0) {
			util.alert(promptDialog);
		} else {
			var body = base.parseForm($(".daModifyDetails"));
			var name = $("#newModifyName").val();
			body.spaceId = spaceId;
			body.name = name;
			base.fnAjax({
				action: "updateAdSpace.json",
				oData: body,
				fnSuccess: function(data) {
					if (data.status == 200) {
						router.route("adManage/platform_main_advertisement_manager.html");
					} else {
						util.alert(data.message);
					}
				}
			});
		}
	};
	//上传图片
	var uploadPicture = function() {
		if ($("#uploadImg").val() != "") {
			$.ajaxFileUpload({
				url: "/finance/api/v1.0/uploadFile.json", //你处理上传文件的服务端
				secureuri: false,
				fileElementId: 'uploadImg',
				dataType: 'json',
				success: function(data) {
					imgSrc = data.data[0].fileUuid;
					$("#meta_img_src").attr("src", "/finance/api/v1.0/downloadFile.file?fileUuid=" + imgSrc);
					$("#meta_img_src").attr("fileUuid", imgSrc);
					$(".img_input").val(data.data[0].fileName);
				},
				complete: function(xml) {
					$("#uploadImg").unbind().on('change', function() {
						uploadPicture();
					});
				},
				error: function(data, status, e) {
					if (data.msg == null) {
						base.alert(2, "文件太大无法上传");
					} else {
						base.alert(2, data.msg);
					}
				}
			});
		} else {
			base.alert(3, "请选择文件！");
		}
	};

	// 点击立刻上传图片
	var clickUpload = function() {
		var successvar = function(data) {
			// var advertId = data.datas[0].advertId;
			var advertId = data.datas[0];
			$("#meta_img_src").attr("src", "");
			$(".img_input").val("");
			$(".link_addr_input").val("");
			$(".play_order_input").val("");
			displayuploadimg(body.sequenze, body.link, body.fileName, imgSrc, advertId);
		};
		var promptDialog = validate.validateInput($("#ad_img_display"));
		if (promptDialog.length != 0) {
			util.alert(promptDialog);
		} else {
			if (imgSrc == null) {
				util.alert("请选择要上传的图片！");
			}
			var body = base.parseForm($(".ad_img_display"));
			body.seqNo = $(".play_order_input").val();
			body.url = $(".link_addr_input").val();
			body.fileName = $(".img_input").val();
			body.fileUuid = imgSrc;
			body.spaceId = spaceId;
			base.fnAjax({
				action: "createAd.json", //接口名称
				oData: body, //传递参数
				fnSuccess: function(data) { //成功回调
					if (data.status == 200) {
						successvar(data);
					} else {
//						base.alert(2, "请选择图片！");
					}
				}
			});
		}
	};

	//点击删除
	var deleteAd = function(adId) {
		base.fnAjax({
			action: "deleteAd.json", //接口名称
			oData: {
				adId: adId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
				} else {
					util.alert("删除广告失败！");
				}
			}
		});
	};

	//	获取已有的广告信息
	var getAdvert = function() {
		var displayimg = function(advert) {
			var str = "";
			var advert = advert;
			var fileUuid = advert.fileUuid;
			var fileName = advert.fileName;
			var link = advert.url;
			var sequenze = advert.seqNo;
			str += '<div class="ad_img_display_ok" adId="' + advert.adId + '" id="' + advert.adId + '">';
			str += '<div class="out_img_size">';
			str += '<div class="left_img_ f14">';
			str += '<img src="/finance/api/v1.0/downloadFile.file?fileUuid=' + fileUuid + '" style="width:120px;height:100px;" />';
			str += '</div>';
			str += '</div>';
			str += '<div class="right_choose_style">';
			str += '<div class="choose_img">';
			str += '<span class="title_set">图片名称：</span>';
			str += '<span class="fileName w100">' + fileName + '</span>';
			str += '<input type="hidden"id="fileUid" value="'+fileUuid+'"/>';
			str += '<span class="modify_button">修改</span>';
			str += '</div>';
			str += '<div class="choose_img">';
			str += '<span class="title_set">轮播顺序：</span>';
			str += '<span class="sequenze w30">' + sequenze + '</span>';
			str += '<span class="delete_button">删除</span>';
			str += '</div>';
			str += '<div class="link_addr">';
			str += '<span class="title_set">链接地址：</span>';
			str += '<div class="f14 url_set">' + link + '</div>';
			str += '</div>';
			str += '</div>';
			str += '<div class="clear"></div>';
			str += '</div>';
			$("#ad_list1").append(str);
		}
		base.fnAjax({
			action: "inquireAdList.json", //接口名称
			oData: {
				spaceId: spaceId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					var advert = data.datas;
					for (var i = 0; i < advert.length; i++) {
						var row = advert[i];
						displayimg(row);
					}
				}
			}
		});
	};
	var displayuploadimg = function(sequenze, link, fileName, imgSrc, advertId) {
		var str = "";
		var advertId = advertId;
		var sequenze = sequenze;
		var link = link;
		var fileUuid = imgSrc;
		var fileName = fileName;
		str += '<div class="ad_img_display_ok" adId="' + advertId + '" id="' + advertId + '">';
		str += '<div class="out_img_size">';
		str += '<div class="left_img_ f14">';
		str += '<img src="/finance/api/v1.0/downloadFile.file?fileUuid=' + fileUuid + '" style="width:120px;height:100px;" />';
		str += '</div>';
		str += '</div>';
		str += '<div class="right_choose_style">';
		str += '<div class="choose_img">';
		str += '<span class="title_set">图片名称：</span>';
		str += '<span class="fileName w100">' + fileName + '</span>';
		str += '<span class="modify_button">修改</span>';
		str += '</div>';
		str += '<div class="choose_img">';
		str += '<span class="title_set">轮播顺序：</span>';
		str += '<span class="sequenze w30">' + sequenze + '</span>';
		str += '<span class="delete_button">删除</span>';
		str += '</div>';
		str += '<div class="link_addr">';
		str += '<span class="title_set">链接地址：</span>';
		str += '<div class="f14 url_set">' + link + '</div>';
		str += '</div>';
		str += '</div>';
		str += '<div class="clear"></div>';
		str += '</div>';
		$("#ad_list1").append(str);
	};
	
	//获取广告位信息
	var getAdSpaceInfo = function() {
		base.fnAjax({
			action: "inquireAdSpaceDetail.json", //接口名称
			oData: {
				spaceId: spaceId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					$("#ad_referenceNo1").val(data.datas[0].referenceNo);
					$("#newModifyName").val(data.datas[0].name);
					$("#ad_length1").val(data.datas[0].length);
					$("#ad_width1").val(data.datas[0].width);
					$("#ad_description1").val(data.datas[0].description);
				}
			}
		});
	};
	return {
		init: init
	};
});